﻿using System.Collections.Generic;
using System.Data;

namespace Extensions
{
    public static class DataExtensions
    {

        public static void RemoveSystemCols(this DataTable table)
        {
            List<string> colsToDelete = new List<string>();
            foreach (DataColumn col in table.Columns)
            {
                string colName = col.ColumnName.Trim().ToLower();
                if (colName == "ınsertedon" || colName == "insertedon" || colName == "modifiedon" || colName == "ts" || colName == "userıd" || colName == "deleted")
                {
                    colsToDelete.Add(col.ColumnName);
                }
            }
            foreach (string colToDelete in colsToDelete)
            {
                table.Columns.Remove(colToDelete);
            }
        }

        public static void MergeColumns(this DataTable t, string MasterColumn, params string[] SlaveColumns)
        {
            foreach (DataRow r in t.Rows)
            {
                string result = "";
                string valMaster = r[MasterColumn].ToString().Trim();
                string valSlave = "";
                foreach (string SlaveCol in SlaveColumns)
                {
                    valSlave = valSlave + r[SlaveCol].ToString().Trim();
                }
                if (valMaster == string.Empty)
                {
                    result = valSlave;
                }
                else if (valSlave == string.Empty)
                {
                    result = valMaster;
                }
                else
                {
                    result = valMaster + " " + valSlave;
                }

                r[MasterColumn] = result;
            }
            foreach (string SlaveCol in SlaveColumns)
            {
                t.Columns.Remove(SlaveCol);
            }

        }

        //public static DataTable BinaryDeserializeToDataTable(this FileInfo fi)
        //{
        //    BinaryFormatter formatter = new BinaryFormatter();
        //    DataTable dt = new DataTable();

        //    if (fi.Exists)
        //        using (FileStream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
        //            dt = (DataTable)formatter.Deserialize(fileStream);

        //    return dt;
        //}

        //public static void BinarySerialize(this DataTable DataTable, string Path)
        //{
        //    DataSet ds = new DataSet();
        //    ds.Tables.Add(DataTable.Copy());            
        //    ds.WriteXml(Path);

        //    BinaryFormatter formatter = new BinaryFormatter();

        //    using (FileStream fileStream = new FileStream(Path, FileMode.Create, FileAccess.Write))
        //    {
        //        formatter.Serialize(fileStream, DataTable);
        //    }
        //}         

        //public static DataTable XmlDeserializeToDataTable(this FileInfo fi)
        //{
        //    DataTable dt = new DataTable();

        //    if (fi.Exists)
        //        using (FileStream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
        //        {
        //            dt.ReadXml(fileStream);                    
        //        }

        //    dt.Columns["ProductId"].ColumnName = "PId";
        //    dt.Columns["PhotoMikro"].ColumnName = "PMikro";

        //    dt.Columns.Add("ProductId", typeof(int));
        //    dt.Columns.Add("PhotoMikro", typeof(byte[]));

        //    foreach (DataRow dr in dt.Select("PId is not null"))
        //    {
        //        dr["ProductId"] = int.Parse(dr["PId"].ToString());
        //        dr["ProductId"] = int.Parse(dr["PId"].ToString());
        //    }

        //    dt.Columns.Remove("PId");

        //    return dt;
        //}

        //public static void XmlSerialize(this DataTable DataTable, string Path)
        //{
        //    if (DataTable.DataSet == null)
        //    {
        //        DataSet ds = new DataSet();
        //        ds.Tables.Add(DataTable);
        //    }

        //    DataTable.DataSet.WriteXml(Path);            
        //}
    }
}